﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace h2100
{
    public class hMapCell:hSprite
    {
        private bool _acceptMousePress = false;
        private string _type = "Unusable";
        public string Type
        {
            get { return _type; }
            set { _type = value; }
        }

        private bool _sel=false;
        public bool Selected
        {
            get { return _sel; }
            set { _sel = value; }
        }

        private bool _fog=true;
        public bool onFogOfWar
        {
            get { return _fog; }
            set { _fog = value; }
        }

        private bool _freeToBuild = true;
        public bool isFreeToBuild        
        {
            get { return _freeToBuild; }
            set { _freeToBuild = value; }
        }

        public hMapCell(ContentManager content, Texture2D[] txt):base(txt)
        {

        }

        public void clearFog(Vector2 point)
        {
            if (point.X > X && point.X < X + Width
                && point.Y > Y && point.Y < Y + Height)
            {
                onFogOfWar = false;
            }
        }

        public void setPlatform(hParams args, string controller)
        {
            if (controller.Equals("player"))
            {
                Texture2D txt = args.hGame.Content.Load<Texture2D>(hGlobal.PathTerrain + "Tile_platform");
                setTexture(txt, 0);
                Type = "BuildingPlatform";
            }
            if (controller.Equals("computer"))
            {
                Texture2D txt = args.hGame.Content.Load<Texture2D>(hGlobal.PathTerrain + "Tile_blight");
                setTexture(txt, 0);
                Type = "EnemyBuildingPlatform";
            }
            isFreeToBuild = false;
        }

        public new void update(hParams args, GameTime gtime)
        {
            MouseState mouse = Mouse.GetState();            

            if (args.IsMouseOverControlPanel)
            {
                return;
            }

            if (mouse.X > X && mouse.X < X + Width
                    && mouse.Y > Y && mouse.Y < Y + Height)
            {
                args.HightlightingMapCell = this;

                if (mouse.LeftButton == ButtonState.Pressed && _acceptMousePress)
                {
                    _acceptMousePress = false;


                    Selected = true;
                }
                else
                {
                    Selected = false;
                }
            }

            if (mouse.LeftButton == ButtonState.Released)
            {
                _acceptMousePress = true;
            }

            base.update(args,gtime);
        }
    }
}
